package dbv;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;

public class ivaCheck {
	protected Shell shell;
	private Text txtIngreseElNombre;
	private Composite composite;
	private Composite composite_2;
	private Composite composite_1;
	private Table table;
	private static String tipo = "";

	/**
	 * Launch the application.
	 * @param args
	 */
	
	public void buscarDatos(String base, String tipoBase, String busqueda, String fecha){
		try {
			Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+base, "", ""); 
			String query="SELECT nombre, DATE_FORMAT(fecha, '%d-%m-%Y') AS fecha, " +
					"CONCAT(sucursal, '-' ,numero) AS factura, " +
					"letra, neto, iva, total, retencion, certificad, tipocomp FROM "+tipoBase+" " +
							"WHERE (nombre LIKE '%"+busqueda+"%') AND (fecha >= '"+fecha+"') " +
									"ORDER BY nombre, DATE(fecha) DESC";
			PreparedStatement ps = conexion.prepareStatement(query);
			ps.execute();
			
			ResultSet rs = ps.getResultSet();
			while (rs.next()){
				TableItem item = new TableItem(table, SWT.NONE);
				
			
					
					if (tipoBase.equals("ventas") && rs.getString(10).equals("8")){
						item.setText(0, rs.getString(1));
						item.setText(1, rs.getString(2));
						item.setText(2, rs.getString(9));
						item.setText(3, "R");
						item.setText(4, "");
						item.setText(5, "");
						item.setText(6, rs.getBigDecimal(8).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());


					}else{
						item.setText(0, rs.getString(1));
						item.setText(1, rs.getString(2));
						if (rs.getString(3) == null){
							item.setText(2, "");
						}else {
							item.setText(2, rs.getString(3));
						}
						item.setText(3, rs.getString(4));
						try {
							item.setText(4, rs.getBigDecimal(5).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
							item.setText(5, rs.getBigDecimal(6).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
							item.setText(6, rs.getBigDecimal(7).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());

						} catch (Exception e) {
							
						}

					}
					
					

				
			}
			ps.close();
			conexion.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					ivaCheck window = new ivaCheck();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	
	
	
	
	
	protected void createContents() {
		shell = new Shell(SWT.MAX | SWT.MIN | SWT.CLOSE);
		//shell.setSize(754, 538);
		shell.setImage(SWTResourceManager.getImage(ivaCheck.class, "/imagenes/AppIcon.png"));
		shell.setText("IVA - Chequear Factura");
		shell.setLayout(new FormLayout());
		

		Rectangle bds = Display.getCurrent().getClientArea();
        Point p = shell.getSize();

        int nLeft = (bds.width - p.x) / 2;
        int nTop = (bds.height - p.y) / 2;

        shell.setBounds(nLeft, nTop, p.x, p.y);
		
		
		
		composite = new Composite(shell, SWT.BORDER);
		FormData fd_composite = new FormData();
		fd_composite.left = new FormAttachment(0, 10);
		fd_composite.right = new FormAttachment(100, -10);
		composite.setLayoutData(fd_composite);
		composite.setLayout(new GridLayout(8, false));
		
		composite_2 = new Composite(shell, SWT.BORDER);
		fd_composite.top = new FormAttachment(0, 10);
		composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));
		FormData fd_composite_2 = new FormData();
		fd_composite_2.right = new FormAttachment(composite, 0, SWT.RIGHT);
		fd_composite_2.top = new FormAttachment(composite, 6);
		fd_composite_2.left = new FormAttachment(0, 10);
		fd_composite_2.bottom = new FormAttachment(100, -68);
		composite_2.setLayoutData(fd_composite_2);
		
		composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setLayout(new FormLayout());
		FormData fd_composite_1 = new FormData();
		fd_composite_1.bottom = new FormAttachment(100, -10);
		fd_composite_1.top = new FormAttachment(composite_2, 6);
		
		table = new Table(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
		table.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.NORMAL));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnProveedor = new TableColumn(table, SWT.NONE);
		tblclmnProveedor.setWidth(250);
		tblclmnProveedor.setText("Proveedor");
		
		TableColumn tblclmnFecha = new TableColumn(table, SWT.NONE);
		tblclmnFecha.setWidth(80);
		tblclmnFecha.setText("Fecha");
		
		TableColumn tblclmnFactura = new TableColumn(table, SWT.NONE);
		tblclmnFactura.setWidth(100);
		tblclmnFactura.setText("Factura");
		
		TableColumn tblclmnTipo = new TableColumn(table, SWT.NONE);
		tblclmnTipo.setWidth(51);
		tblclmnTipo.setText("Tipo");
		
		TableColumn tblclmnSubtotal = new TableColumn(table, SWT.RIGHT);
		tblclmnSubtotal.setWidth(85);
		tblclmnSubtotal.setText("Subtotal");
		
		TableColumn tblclmnIva = new TableColumn(table, SWT.RIGHT);
		tblclmnIva.setWidth(77);
		tblclmnIva.setText("IVA");
		
		TableColumn tblclmnTotal = new TableColumn(table, SWT.RIGHT);
		tblclmnTotal.setWidth(84);
		tblclmnTotal.setText("Total");
		fd_composite_1.right = new FormAttachment(composite, 0, SWT.RIGHT);
		fd_composite_1.left = new FormAttachment(0, 10);
		composite_1.setLayoutData(fd_composite_1);
		
		Button btnSalir = new Button(composite_1, SWT.NONE);
		btnSalir.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		btnSalir.setAlignment(SWT.RIGHT);
		btnSalir.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		btnSalir.setImage(SWTResourceManager.getImage(ivaCheck.class, "/imagenes/delete-icon.png"));
		FormData fd_btnSalir = new FormData();
		fd_btnSalir.top = new FormAttachment(0, 7);
		fd_btnSalir.right = new FormAttachment(100);
		fd_btnSalir.bottom = new FormAttachment(100, -8);
		fd_btnSalir.left = new FormAttachment(100, -107);
		btnSalir.setLayoutData(fd_btnSalir);
		btnSalir.setText("Salir");
		
		Label lblProveedor = new Label(composite, SWT.NONE);
		lblProveedor.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblProveedor.setText("Proveedor:");
		lblProveedor.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		
		txtIngreseElNombre = new Text(composite, SWT.BORDER);
		txtIngreseElNombre.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				txtIngreseElNombre.setText("");
			}
		});
		txtIngreseElNombre.setForeground(SWTResourceManager.getColor(0, 0, 0));
		txtIngreseElNombre.setText("Ingrese el nombre completo o parte.");
		txtIngreseElNombre.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		GridData gd_txtIngreseElNombre = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_txtIngreseElNombre.widthHint = 355;
		gd_txtIngreseElNombre.heightHint = 22;
		txtIngreseElNombre.setLayoutData(gd_txtIngreseElNombre);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		
		Label lblEmpresa = new Label(composite, SWT.NONE);
		lblEmpresa.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		GridData gd_lblEmpresa = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
		gd_lblEmpresa.widthHint = 57;
		gd_lblEmpresa.heightHint = 19;
		lblEmpresa.setLayoutData(gd_lblEmpresa);
		lblEmpresa.setText("Empresa:");
		
		final Combo comboEmpresa = new Combo(composite, SWT.READ_ONLY);
		comboEmpresa.setVisibleItemCount(12);
		comboEmpresa.setItems(new String[] {"SELECCIONAR EMPRESA.......", "AZTIRIA ELSA CLARA", "CLAVIER", 
				"ESTANCIA EL ALBARDON S.A.", "ESTANCIA PAMPA GRANDE S.A.", "ISABEL DUGGAN DE DI CAMPELLO", 
				"KENVIS S.A.", "LOS HUAICOS M.L. S.A.", "TRES ISLAS S.A."});
		
		comboEmpresa.setData("AZTIRIA ELSA CLARA","aztiriaiva"); 
		comboEmpresa.setData("CLAVIER","clavieriva"); 
		comboEmpresa.setData("ESTANCIA EL ALBARDON S.A.","albardoniva"); 
		comboEmpresa.setData("ESTANCIA PAMPA GRANDE S.A.","pampaiva"); 
		comboEmpresa.setData("ISABEL DUGGAN DE DI CAMPELLO","dugganiva"); 
		comboEmpresa.setData("KENVIS S.A.","kenvisiva"); 
		comboEmpresa.setData("LOS HUAICOS M.L. S.A.","huaicosiva"); 
		comboEmpresa.setData("TRES ISLAS S.A.","tisaiva"); 
		
		comboEmpresa.select(0);

		comboEmpresa.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		GridData gd_comboEmpresa = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_comboEmpresa.widthHint = 340;
		gd_comboEmpresa.heightHint = 27;
		comboEmpresa.setLayoutData(gd_comboEmpresa);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		
		Label lblPeriodo = new Label(composite, SWT.NONE);
		lblPeriodo.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblPeriodo.setText("Fecha:");
		lblPeriodo.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		
		final Text fecha = new Text(composite, SWT.BORDER);
		fecha.setToolTipText("Formato: 01/01/2000");
		fecha.setForeground(SWTResourceManager.getColor(0, 0, 0));
		
		

		fecha.addVerifyListener(new VerifyListener() {
			public void verifyText(VerifyEvent event) {
		        event.doit = false;
		        // Allow backspace and delete
		        if (event.character == '\u0008' || event.character == '\u007F' || event.character == '\u002F')	event.doit = true;
		        if (Character.isDigit(event.character)) event.doit = true; 
		        }
		});
		fecha.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		GridData gd_fecha = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_fecha.heightHint = 22;
		gd_fecha.widthHint = 150;
		fecha.setLayoutData(gd_fecha);
		
		fecha.setTextLimit(10);
		
		final Button btnCompra = new Button(composite, SWT.RADIO);
		btnCompra.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		btnCompra.setText("Compra");
		
		final Button btnVenta = new Button(composite, SWT.RADIO);
		btnVenta.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		btnVenta.setText("Venta");
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		
		Button buscarButton = new Button(composite, SWT.NONE);
		buscarButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				
				table.removeAll();
				
				if (btnCompra.getSelection()==true){
					tipo = "compras";
				}
				if (btnVenta.getSelection()==true){
					tipo = "ventas";
				}
				buscarDatos(comboEmpresa.getData(comboEmpresa.getItem(comboEmpresa.getSelectionIndex())).toString(), tipo, txtIngreseElNombre.getText(), new Utility().fechaParse(fecha.getText()));
				table.setFocus();
			}
		});
		GridData gd_buscarButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
		gd_buscarButton.heightHint = 32;
		gd_buscarButton.widthHint = 85;
		buscarButton.setLayoutData(gd_buscarButton);
		buscarButton.setText("Buscar");
		
		shell.setDefaultButton(buscarButton);
		composite.setTabList(new Control[]{txtIngreseElNombre, comboEmpresa, fecha, btnCompra, btnVenta, buscarButton});

		
	}
}
